#include "skip_file.h"
#include <stdio.h>

size_t s_filesize(const char* file)
{
	size_t ret;
	FILE* fp = NULL;
	if(file==NULL)	return 0;
	fp = fopen(file, "r");
	if(fp==NULL)	return 0;
	fseek(fp, 0L, SEEK_END);
	ret=ftell(fp);
	fclose(fp);

	return ret;
}

int s_create(const char* file)
{
	int ret;
	ret = open(file, O_RDWR|O_BINARY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
	if(ret<0){
		throw_error("create error!\n");
	}
	return ret;
}

int s_open(const char* file)
{
	int ret;
	ret = open(file, O_RDWR|O_BINARY);
	if(ret<1){
		throw_error("open error!\n");
	}
	return ret;
}

void s_close(int fd)
{
	if(close(fd)<0){
		throw_error("close error!\n");
	}
}

void s_write(int fd, RECPOS start, byte* buf, size_t len)
{
	RECPOS err;
	err = start - lseek(fd, start, SEEK_SET);
	if(err == 0)
		err = len - write(fd, buf, len);
	if(err !=0) {
		throw_error("write error!\n");
	}
}

void s_read(int fd, RECPOS start, byte* buf, size_t len)
{
	RECPOS err;
	err = start - lseek(fd, start, SEEK_SET);
	if(err == 0)
		err = len - read(fd, buf, len);
	if(err != 0) {
		throw_error("read error!\n");
	}
}


